package com.tg.base.concurrent;

import java.util.concurrent.*;

public class JavaConcurrentShare {

    public static void main2(String[] args) throws ExecutionException, InterruptedException {
        String a = "a";

        FutureTask<String> target = new FutureTask<String>(() -> {
            try {
                TimeUnit.SECONDS.sleep(10);
                return "abc";

            } catch (Exception ex) {
                return "";
            }
        });

        Thread thread = new Thread(target);
        thread.start();

//        String value = target.get();
        String value = "";

        try {
            value = target.get(1, TimeUnit.SECONDS);
        } catch (TimeoutException ex) {
            value = "timeout";
        }

//        target.cancel()


        System.out.println(value);

//        ExecutorService executor = Executors.newFixedThreadPool(1);
//        executor.submit(target);
//        submit.get()

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Integer lock = 0;

        FutureTask<String> target = new FutureTask<String>(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + ":still running");
//                synchronized (lock) {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                        System.out.println(Thread.currentThread().getName() + ":done");

                    } catch (Exception ex) {
                        return "";
                    }
//                }
            }
//            return "abc";

        });

        Thread thread1 = new Thread(target);
        Thread thread2 = new Thread(target);
        thread1.start();
        thread2.start();

        TimeUnit.SECONDS.sleep(1);

//        target.cancel(false);

        TimeUnit.SECONDS.sleep(200);
    }
}
